package hips.images.integer.gray8;

import hips.images.integer.PixelValueInt;

public class PixelValueGray8 implements PixelValueInt<PixelValueGray8> {

	private int[] slices;

	public PixelValueGray8(int n) {
		slices = new int[n];
	}

	public void setLower(PixelValueGray8 p) {
		for (int i = 0; i < slices.length; i++) {
			if (p.slices[i] < slices[i]) {
				slices[i] = p.slices[i];
			}
		}
	}

	public void setGreater(PixelValueGray8 p) {
		for (int i = 0; i < slices.length; i++) {
			if (p.slices[i] > slices[i]) {
				slices[i] = p.slices[i];
			}
		}
	}

	public boolean isEqual(PixelValueGray8 p) {
		for (int i = 0; i < slices.length; i++) {
			if (p.slices[i] != slices[i]) {
				return false;
			}
		}
		return true;
	}

	public boolean isGreaterOrEqual(PixelValueGray8 p) {
		for (int i = 0; i < slices.length; i++) {
			if (p.slices[i] > slices[i]) {
				return false;
			}
		}
		return true;
	}

	public boolean isLowerOrEqual(PixelValueGray8 p) {
		for (int i = 0; i < slices.length; i++) {
			if (p.slices[i] < slices[i]) {
				return false;
			}
		}
		return true;
	}

	public PixelValueGray8 range(PixelValueGray8 p) {
		PixelValueGray8 pRange = new PixelValueGray8(slices.length);
		for (int i = 0; i < slices.length; i++) {
			pRange.slices[i] = Math.abs(slices[i] - p.slices[i]);
		}
		return pRange;
	}
	
	public PixelValueGray8 range(PixelValueGray8 pvalue1, PixelValueGray8 pvalue2) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] = Math.abs(pvalue1.slices[i] - pvalue2.slices[i]);
		}
		return this;
	}

	public int maxValueInt() {
		int max = slices[0];
		for (int i = 1; i < slices.length; i++) {
			if (max < slices[i]) {
				max = slices[i];
			}
		}
		return max;
	}

	public int minValueInt() {
		int min = slices[0];
		for (int i = 1; i < slices.length; i++) {
			if (min > slices[i]) {
				min = slices[i];
			}
		}
		return min;
	}

	public String getString() {
		if (slices.length == 1){
			return getString(0);
		}
		String returned = "(";
		for (int i = 0; i < slices.length - 1; i++) {
			returned += getString(i) + ",   ";
		}
		return returned + getString(slices.length - 1) + ")";
	}

	public String getString(int index) {
		return "" + slices[index];
	}

	public void setValueInt(int value, int slice) {
		slices[slice] = value;
	}

	public int getValueInt(int slice) {
		return slices[slice];
	}

	public int getSlices() {
		return slices.length;
	}

	public PixelValueGray8 add(PixelValueGray8 p) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] += p.slices[i];
		}
		return this;
	}

	public PixelValueGray8 add(float value) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] += value;
		}
		return this;
	}

	public PixelValueGray8 sub(PixelValueGray8 p) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] -= p.slices[i];
		}
		return this;
	}

	public PixelValueGray8 sub(float value) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] -= value;
		}
		return this;
	}

	public PixelValueGray8 mult(float value) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] *= value;
		}
		return this;
	}

	public PixelValueGray8 div(float value) {
		for (int i = 0; i < slices.length; i++) {
			slices[i] /= value;
		}
		return this;
	}

	public PixelValueGray8 copy() {
		PixelValueGray8 p = new PixelValueGray8(slices.length);
		System.arraycopy(slices, 0, p.slices, 0, slices.length);
		return p;
	}

	public void setValueInt(int value, int slice, int channel) {
		setValueInt(value, slice);
	}

	public int getValueInt(int slice, int channel) {
		return getValueInt(slice);
	}

	public int sumInt() {
		int sum = 0;
		for (int i = 0; i < slices.length; i++){
            sum += slices[i];
        }
		return sum;
	}

	public void setValue(Number value, int slice) {
		setValueInt(value.intValue(), slice);
	}

	public void setValue(Number value, int slice, int channel) {
		setValueInt(value.intValue(), slice, channel);
	}

	public Integer getValue(int slice) {
		return getValueInt(slice);
	}

	public Integer getValue(int slice, int channel) {
		return getValueInt(slice, channel);
	}

	public Integer minValue() {
		return minValueInt();
	}

	public Integer maxValue() {
		return maxValueInt();
	}

	public Integer sum() {
		return sumInt();
	}
}